package com.example.edianzu.somur.utils.manager;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.example.edianzu.somur.App;

/**
 * author：风不会停息 on 2017/7/22 18:19
 * mailbox：yh131412hys@163.com
 * project：Somur
 * tel：13295355900
 * describe：
 */

public class PackageInfoManager {

    private static final int VERSION_CODE = 0x10;
    private static final int VERSION_NAME = 0x11;
    private static final int APP_NAME = 0x12;
    private static PackageInfoManager packageInfoManager = null;
    private PackageInfoManager(){}
    public static synchronized PackageInfoManager getInstance() {
        if (packageInfoManager == null) {
            packageInfoManager = new PackageInfoManager();
        }
        return packageInfoManager;
    }

    /**
     * 获取版本名称
     *
     * @return 版本名称
     */
    public String getVersionInfo(int infoType) {
        //获取包管理器
        PackageManager pm = App.getApp().getPackageManager();
        PackageInfo packageInfo = null;
        try {
            //获取包信息
            packageInfo = pm.getPackageInfo(App.getApp().getPackageName(), 0);
            //返回版本号
            if (infoType == VERSION_CODE){
                return String.valueOf(packageInfo.versionCode);
            }else if (infoType == VERSION_NAME){
                return packageInfo.versionName;
            }else if (infoType == APP_NAME){
                return App.getApp().getString(packageInfo.applicationInfo.labelRes);
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
